<?php

namespace App\Models;


use App\Service\Base\Model;

class SiteVersion extends Model
{

    // 广告管理
    protected $table = 'site_version';

    // 缓存字段，用于搜索绑定的字段
    protected $cacheFields = ['id', 'platform', 'code',];

    // 获取所有的广告信息
    public function getAll($where = [])
    {
        // 获取缓存
        $bang     = $this->bang;
        $is_cache = $this->is_cache || $bang;
        $args     = compact('where');
        if($is_cache){
            $cacheName = $this->cacheKey('lists', $args);
            $cacheData = $this->cacheClass()->getCache($cacheName);
            if(!empty($cacheData)){
                // 检查是否已经绑定
                $this->checkBangList($cacheData, $cacheName);

                return $cacheData;
            }
        }

        // 查询
        $result = [];
        $lists  = $this->where($where)->get()->toArray();
        /*foreach($lists as $item){
            $channel = $item['channel'];
            $space   = $item['space'];
            if($channel){
                $result[$channel][$space] = $item;
            } else{
                $result[$space] = $item;
            }
        }*/

        // 保存为缓存
        if($is_cache){
            $this->addCacheList($lists, $cacheName, $result, 'list');
        }

        return $result;
    }

    // 定义验证操作
    public function rules()
    {

        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            'platform' => 'required|in:ios,android',
            'code'     => 'required',
            'remark'   => 'required',
            //'url'    => 'required',
        ];


        $msg = [
            'id'       => '自编号',
            'platform' => '平台',
            'code'     => '版本号',
            'name'     => '名称',
            'url'      => '更新地址',
            'remark'   => '更新备注',
            'ctime'    => '创建时间',
            'mtime'    => '修改时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add];
    }
}
